<!DOCTYPE html>
<html>
<head>
<title>eventBus</title>
<script src="js/vue.js"></script>
</head>
<body>
<div id="todo-app">
<h1>todo app</h1>
<new-todo></new-todo>
<todo-list></todo-list>
</div>
<script>
var eventHub = new Vue( {
	data(){
		return{
		todos:['A','B','C']
		}
	},
	created:function () {
		this.$on('add', this.addTodo)
		this.$on('delete', this.deleteTodo)
	},
	beforeDestroy:function () {
		this.$off('add', this.addTodo)
		this.$off('delete', this.deleteTodo)
	},
	methods: {
		addTodo: function (newTodo) {
		this.todos.push(newTodo)
		},
		deleteTodo: function (i) {
		this.todos.splice(i,1)
		}
	}
})
var newTodo = {
	template:`<div><input type="text" autofocus v-model="newtodo"/><button @click="add">add</button></div>`,
	data(){
			return{
			newtodo:''
			}
	},
	methods:{
		add:function(){
		eventHub.$emit('add', this.newtodo)
		this.newtodo = ''
		}
	}
}

var todoList = {
	template:`<ul><li v-for="(index,item) in items">{{item}} \
	          <button @click="rm(index)">X</button></li> \
	          </ul>`,
    data(){
	          return{
	          items:eventHub.todos
	          }
          },
   methods:{
	          rm:function(i){
	          eventHub.$emit('delete',i)
	          }
          }
	  }
var app= new Vue({
el:'#todo-app',
	components:{
		newTodo:newTodo,
		todoList:todoList
	}
})
</script>
</body>
</html>